package br.com.uniban.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.com.uniban.bo.QuestaoBO;
import br.com.uniban.dto.QuestaoDTO;
import br.com.uniban.entity.Disciplina;
import br.com.uniban.entity.QuestoesAvulsas;
import br.com.uniban.entity.Usuario;
import br.com.uniban.form.QuestaoForm;
import br.com.uniban.form.UsuarioForm;

import com.google.gson.Gson;


public class QuestaoAction extends DispatchAction {
	
	private QuestaoBO bo = null;
	private static final String INICIO = "inicio";
	private static final String PESQ = "pesqInicio";
	
	public QuestaoAction(){
		try {
			if(bo == null)
				bo = new QuestaoBO();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ActionForward inicio(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {	
		QuestaoForm form = (QuestaoForm) actionForm;
		form.getQuestao().setResposta('0');
		List<Disciplina> listaDisciplina = (List<Disciplina>) request.getSession().getAttribute(UsuarioAction.LISTA_DISCIPLINA);
		form.setListaDisciplina(listaDisciplina);
		return mapping.findForward(INICIO);
	}
	
	public ActionForward cadastrar(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QuestaoForm form = (QuestaoForm) actionForm;
		UsuarioForm usuarioForm = (UsuarioForm) request.getSession().getAttribute(UsuarioAction.USUARIO_FORM);
		form.getQuestao().setUsuario(usuarioForm.getUsuario());
		bo.cadastraQuestao(form.getQuestao());
		return null;
	}
	public ActionForward pesqInicio(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {	
		return mapping.findForward(PESQ);
	}
	public ActionForward pesquisar(ActionMapping mapping,ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		QuestaoForm form = (QuestaoForm) actionForm;
		QuestaoDTO dto = form.getFiltro();
		if(Boolean.TRUE.equals(dto.getFlMinhasPerguntas())){
			Usuario usuario = UsuarioAction.getUsuario(request);
			dto.setIdUsuario(usuario.getId());
		}
		List<QuestoesAvulsas> lista = bo.pesquisaQuestao(dto);

		for (QuestoesAvulsas q : lista) {
			q.setAlternativa_A(StringEscapeUtils.escapeHtml4(q.getAlternativa_A()));
			q.setAlternativa_B(StringEscapeUtils.escapeHtml4(q.getAlternativa_B()));
			q.setAlternativa_C(StringEscapeUtils.escapeHtml4(q.getAlternativa_C()));
			q.setAlternativa_D(StringEscapeUtils.escapeHtml4(q.getAlternativa_D()));
			q.setPergunta(StringEscapeUtils.escapeHtml4(q.getPergunta()));
			
		}
		Gson gson = new Gson();
		String json = gson.toJson(lista);
		response.getWriter().write(json);
		return null;
	}
}
